package ms.unicorn.dsl.utils;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class KeyList<Ktype, Vtype> extends
		ArrayList<java.util.Map.Entry<Ktype, Vtype>> {

	private static final long serialVersionUID = 1L;

	public int indexOfKey(Object key) {
		for (int i = 0; i < this.size(); i++)
			if (this.get(i).getKey().equals(key)) return i;
		return -1;
	}
	
	public boolean containsKey(Object key) {
		return indexOfKey(key) == -1;
	}

	public Vtype getByKey(Object key) {
		for (Map.Entry<Ktype, Vtype> e: this)
			if (e.getKey().equals(key)) return e.getValue();
		return null;
	}

	public Vtype put(Ktype key, Vtype value) {
		int idx = indexOfKey(key);
		if (idx == -1)
			super.add(new AbstractMap.SimpleEntry<Ktype, Vtype>(key, value));
		else
			get(idx).setValue(value);
		return value;
	}
	
	public Vtype add(Ktype key, Vtype value) {
		return put(key, value);
	}

	public Collection<Ktype> keys() {
		List<Ktype> keys = new ArrayList<Ktype>(size());
		for (Map.Entry<Ktype, Vtype> e: this)
			keys.add(e.getKey());
		return keys;
	}

	public Collection<Vtype> values() {
		List<Vtype> values = new ArrayList<Vtype>(size());
		for (Map.Entry<Ktype, Vtype> e: this)
			values.add(e.getValue());
		return values;
	}

}
